﻿using System;
using System.Linq;
using System.Web.Mvc;
using Solution.Business;
using Solution.Data;
using Solution.Utilities;
using Solution.Utilities.DynamicQueryable;
using Solution.Business.Infrastructure;

namespace Solution.Mvc.Areas.Admin.Controllers
{
    [Authorize]
    public class SysConfigRoleGroupController : Controller
    {
        PolicyBLL Repository = new PolicyBLL();
        [CAuthorize(Module = "system", Functions = "V")]
        public ActionResult Index()
        {
            ViewBag.group = new GroupBLL().GetAll().Where(t => t.flag != "D");
            return View();
        }
        public JsonResult GetData(bool? showBin, int? GroupID, string sidx, string sord, int page, int rows)
        {
            bool _showBin = showBin ?? false;
            int _GroupID = GroupID ?? 1;
            int pageIndex = Convert.ToInt32(page) - 1;
            int pageSize = rows;
            var temp = Repository.GetAll().Where(p => p.GroupId == _GroupID);
            int totalRecords = Repository.GetAll().Where(t => t.flag != "D").Where(p => p.GroupId == _GroupID).Count();
            int totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
            var questions = Repository.GetAll().Where(t => t.flag != "D").Where(p => p.GroupId == _GroupID).OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize).ToList();
            if (_showBin)
            {

                totalRecords = Repository.GetAll().Where(t => t.flag == "D").Where(p => p.GroupId == _GroupID).Count();
                totalPages = (int)Math.Ceiling((float)totalRecords / (float)pageSize);
                questions = Repository.GetAll().Where(t => t.flag == "D").Where(p => p.GroupId == _GroupID).OrderBy(sidx + " " + sord).Skip(pageIndex * pageSize).Take(pageSize).ToList();
            }
            var jsonData = new
            {
                total = totalPages,
                page,
                records = totalRecords,
                rows = (from question in questions
                        select new
                        {
                            i = question.Id.ToString(),
                            cell = new string[] { 
                                question.Id.ToString(), 
                                question.Module.Name.ToString(), 
                                question.View.ToString(),  
                                question.Insert.ToString(), 
                                question.Delete.ToString(), 
                                question.Update.ToString(), 
                                question.All.ToString(), 
                                question.flag.ToString() }
                        }).ToArray()
            };
            return Json(jsonData, JsonRequestBehavior.AllowGet);
        }
        [CAuthorize(Module = "system", Functions = "U")]
        public JsonResult Edit(string oper, bool? delForever, string[] id, bool View, bool Insert, bool Delete, bool Update, bool All, FormCollection collection)
        {
            string[] ids = id;
            bool _delForever = delForever ?? false;
            switch (oper)
            {
                case "edit":
                    bool _flag = false;
                    Policy myfentity = Repository.GetOne(ConvertUtility.ToInt(ids[0]));
                    if (myfentity.View != View) { _flag = true; myfentity.View = View; };
                    if (myfentity.Insert != Insert) { _flag = true; myfentity.Insert = Insert; };
                    if (myfentity.Delete != Delete) { _flag = true; myfentity.Delete = Delete; };
                    if (myfentity.Update != Update) { _flag = true; myfentity.Update = Update; };
                    if (myfentity.All != All) { _flag = true; myfentity.All = All; };
                    if (Repository.Update(myfentity))
                    {
                        if (_flag)
                            return Json("ok", JsonRequestBehavior.AllowGet);
                        return Json("not", JsonRequestBehavior.AllowGet);
                    }
                    return Json("err", JsonRequestBehavior.AllowGet);

                default:
                    return Json("err", JsonRequestBehavior.AllowGet);
            }

        }
    }
}
